package com.test.baseGroovy

/**
 * StringTest--GroovyTest
 *
 * @author: Jovi
 *
 * @createTime: 2018-06-14 10:47
 *
 * */
String sample = "Hello world"
println sample[1..2]
println sample[4..2]
println sample[-2]

String a = "Hello "
String b = "world"
println sample.equals(a+b)
println a*3

println a.center(15)//返回一个新的长度为numberOfChars的字符串，该字符串由左侧和右侧用空格字符填充的收件人组成

println a.concat(b) //将指定的String连接到此String的结尾。

a.eachMatch("."){ //处理每个正则表达式组匹配的给定String的子字符串。
    println it
}

println a.endsWith("lo ")//测试此字符串是否以指定的后缀结尾。

println a.indexOf('e')
println a.indexOf('l',1)//返回此字符串中指定字符第一次出现的索引，在指定索引处开始搜索，如果字符未出现，则返回-1。

println a.indexOf('el')

println sample.matches("Hello")
println sample.matches("Hello.*")

println sample.minus("or")

println sample.next()
println sample.previous()

println a.padLeft(10)
println a.padLeft(10,"*")
println a.padRight(10)
println a.padRight(10,"*")

println a.plus(b)

println a.reverse()

c = "Hello-world"
def split = c.split('-')
for (String c1  : split) {
    println c1
}

def sParameterLessClosure = "1+2 == ${-> 3}"
assert sParameterLessClosure == '1+2 == 3'

def sOneParaClosure = "1+2==${w -> w << 3}"
assert sOneParaClosure == '1+2==3'


/* 个普通插值表达式值替换实际是在GString创建的时刻
，一个包含闭包的表达式由于延迟运算调运toString()方法，所以会产生一个新的字符串值。*/
def number = 1
def eagerGString = "value == ${number}"
def lazyGString = "value == ${-> number}"

println eagerGString
println lazyGString

number = 2
println eagerGString
println lazyGString


// 由于相同字符串的String与GString的HashCode不同，所以我们一定要避免使用GString作为MAP的key
def key = "a"
def m = ["${key}" : "letter ${key}"]
println m["a"] == null

def fooPattern = /.*foo*./
println fooPattern


char c1 = 'A'
println c1 instanceof Character

def c2 = 'B' as char
println c2 instanceof Character

def c3 = (char) 'C'
println c3 instanceof Character



